import './App.css';
import React from 'react';
import LineHeight2 from './LineHeight2';
import LineWeight2 from './LineWeight2';
import LineHead2 from './LineHead2';
import { List, Tabs, WhiteSpace, Toast } from 'antd-mobile';
import * as request from './request';
import { connect } from 'react-redux';

const tabs = [
  { title: '身高' },
  { title: '体重' },
  { title: '头围' },
];
const now = new Date();
const apiURL = "/api/services/app/Edu_EventGrowth/GetList?SkipCount=0&MaxResultCount=100";
const apiBzUrl = "/api/services/app/Edu_PrematureBabyCurve/GetList";
const apiURLChild = "/api/services/app/Edu_Children/Get?id=";
const apiURLBMI = "/api/services/app/Edu_BMI/GetAll?skipCount=0&maxResultCount=200";
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      growthData: [],
      growthBzData: [],
      babyData: {
        weight: 0,
        gweek: 0,
        correctGestionalAge: 0,
        correctAge: 0
      },
      show: false,
      config: {
        type: 'one'
      },
      startTime: now,
      dataBMI: []
    }
  }
  componentDidMount() {
    var cid = this.props.match.params.childid;
    if (!cid) {
      Toast.info('请先登录');
      this.props.history.push('/Login');
    }
    else {
      this.fetchBMI();
      this.fetchData();
    }
  }
  fetchData = () => {
    var cid = this.props.match.params.childid;
    this.getBabyData(cid)
  }

  getBabyData = (cid) => {
    if (cid) {
      var apiurl = apiURL + '&childId=' + cid + '&eType=basic';
      request.get(apiURLChild + cid).then((data) => {
        var babyData = data.result;
        this.setState({ babyData });
        request.get(apiurl)
          .then((data) => {
            var minAge = 99999;
            var maxAge = 0;
            var growthData = data.result.items
            var sex = babyData.sex === "男" ? 1 : 0;
            var btype = babyData.correctGestionalAge < 40 ? 0 : 1; //早产儿是0
            this.setState({ growthData });
            if (btype === 0) {
              minAge = 0;
              maxAge = 50;
            }
            else {
              minAge = 0;
              maxAge = 36;
            }
            request.get(apiBzUrl + "?sex=" + sex + "&btype=" + btype + "&minAge=" + minAge + "&maxAge=" + maxAge).then((response) => {
              var growthBzData = response.result.items;
              this.setState({ growthBzData });
            });

          })
      });
    }
    else {
      Toast.info('请选择宝宝');
    }
  }

  fetchBMI = () => {
    request.get(apiURLBMI).then((data) => {
      this.setState({ dataBMI: data.result.items });
    })
  }
  selectBaby = (babyData) => {
    this.setState({ babyData })
    this.getBabyData(babyData.id)
  }
  render() {
    const { growthData, growthBzData, babyData, dataBMI } = this.state;
    return (
      <div className="App">
        <Tabs tabs={tabs}
          initialPage={1}
          onChange={(tab, index) => { }}
          onTabClick={(tab, index) => { }}
        >
          <div>
            <WhiteSpace />
            <LineHeight2 growthData={growthData} growthBzData={growthBzData} babyData={babyData} />
          </div>
          <div>
            <WhiteSpace />
            <LineWeight2 growthData={growthData} growthBzData={growthBzData} babyData={babyData} />
          </div>
          <div>
            <WhiteSpace />
            <LineHead2 growthData={growthData} growthBzData={growthBzData} babyData={babyData} />
          </div>
        </Tabs>
      </div>
    )
  };
}
export default connect(
  state => ({ currentUser: state.setCurrentUser, currentChild: state.setCurrentChild })
)(App)
